package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Menu;
import com.embhealth.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * menu控制层
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 根据用户id获取权限url
     */
    @RequestMapping("/findByUsername")
    public Result findByUsername(String username) {
        List<Map<String, Object>> menuList = menuService.findByUsername(username);
        return new Result(true, "菜单栏初始化成功", menuList);
    }

    /**
     * 分页查询 展示所有菜单数据
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findPage(queryPageBean);
    }

    /**
     * 根据menu id回显菜单数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/handleUpdate")
    public Result handleUpdate(Integer id) {
        try {
            Menu menu = menuService.handleUpdate(id);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 用于角色页面的数据回显分页查询
     */
    @RequestMapping("/queryPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.queryPage(queryPageBean);
    }
    /**
     * 根据role_id查询所有菜单权限，并且排序
     *
     * @param id
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllMenuByRole")
    public PageResult findAllPmsByRole(Integer id, @RequestBody QueryPageBean queryPageBean) {
        return menuService.findAllMenByRole(id, queryPageBean);
    }

    /**
     * 添加菜单
     *
     * @return
     */
    @RequestMapping("/handleAdd")
    public Result handleAdd(@RequestBody Menu menu) {
        try {
            menuService.handleAdd(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 编辑更新菜单项
     *
     * @param menu
     * @return
     */
    @RequestMapping("/handleEdit")
    public Result handleEdit(@RequestBody Menu menu) {
        try {
            menuService.handleEdit(menu);
            return new Result(true, MessageConstant.UPDATE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MENU_FAIL);
        }
    }

    /**
     * 根据id删除菜单项
     *
     * @param id
     * @return
     */
    @RequestMapping("/handleDelete")
    public Result handleDelete(Integer id) {
        boolean result = menuService.handleDelete(id);
        if (result) {
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } else {
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
